package com.sokal.easycare.modules.position.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.modules.position.entity.DutyApply;
import com.sokal.easycare.modules.position.service.DutyApplyService;
import com.sokal.easycare.modules.sys.entity.FileLibrary;
import com.sokal.easycare.modules.sys.entity.UserRealnameAuth;
import com.sokal.easycare.modules.sys.service.FileLibrayService;
import com.sokal.easycare.modules.sys.service.UserRealnameAuthService;

/**
 * 岗位申请Controller
 * 
 * @author yangmin
 * @version 2017-04-24
 */
@Controller
@RequestMapping(value = "${adminPath}/position/dutyApply")
public class DutyApplyController extends BaseController {

	@Autowired
	private DutyApplyService dutyApplyService;

	@Autowired
	private UserRealnameAuthService userRealnameAuthService;

	@Autowired
	FileLibrayService fileLibrayService;

	@ModelAttribute
	public DutyApply get(@RequestParam(required = false) String id) {
		DutyApply entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = dutyApplyService.get(id);
		}
		if (entity == null) {
			entity = new DutyApply();
		}
		return entity;
	}

	@RequiresPermissions("position:dutyApply:view")
	@RequestMapping(value = { "list", "" })
	public String list(DutyApply dutyApply, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DutyApply> page = dutyApplyService.findPage(new Page<DutyApply>(request, response), dutyApply);
		model.addAttribute("page", page);
		return "modules/position/dutyApplyList";
	}

	@RequiresPermissions("position:dutyApply:view")
	@RequestMapping(value = "form")
	public String form(DutyApply dutyApply, Model model) {
		model.addAttribute("dutyApply", dutyApply);
		UserRealnameAuth userRealnameAuth = new UserRealnameAuth();
		userRealnameAuth.setUser(dutyApply.getUser());
		List<UserRealnameAuth> list = userRealnameAuthService.findList(userRealnameAuth);
		if(!list.isEmpty())
		{
			userRealnameAuth = list.get(0);
			model.addAttribute("file1path", Global.getConfig("fastdfs_file_url")+fileLibrayService.get(userRealnameAuth.getFile1()).getPath());
			model.addAttribute("file2path", Global.getConfig("fastdfs_file_url")+fileLibrayService.get(userRealnameAuth.getFile2()).getPath());
			model.addAttribute("file3path", Global.getConfig("fastdfs_file_url")+fileLibrayService.get(userRealnameAuth.getFile3()).getPath());
			model.addAttribute("userRealnameAuth", userRealnameAuth);
		}
		FileLibrary fileLibrary = new FileLibrary();
		fileLibrary.setBId(dutyApply.getId());
		List<FileLibrary> files = fileLibrayService.findList(fileLibrary);
		for (FileLibrary file : files) {
			file.setPath( Global.getConfig("fastdfs_file_url")+file.getPath());
		}
		model.addAttribute("files", files);
		return "modules/position/dutyApplyForm";
	}

	@RequiresPermissions("position:dutyApply:edit")
	@RequestMapping(value = "save")
	public String save(DutyApply dutyApply, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, dutyApply)) {
			return form(dutyApply, model);
		}
		dutyApplyService.save(dutyApply);
		addMessage(redirectAttributes, "保存岗位申请成功");
		return "redirect:" + Global.getAdminPath() + "/position/dutyApply/?repage";
	}

	@RequiresPermissions("position:dutyApply:edit")
	@RequestMapping(value = "delete")
	public String delete(DutyApply dutyApply, RedirectAttributes redirectAttributes) {
		dutyApplyService.delete(dutyApply);
		addMessage(redirectAttributes, "删除岗位申请成功");
		return "redirect:" + Global.getAdminPath() + "/position/dutyApply/?repage";
	}

}